{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cK9q7DEQlmJ1"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/jeffheaton/app_deep_learning/blob/main/assignments/assignment_yourname_class9.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sxyItFumlmJ4"
      },
      "source": [
        "# T81-558: Applications of Deep Neural Networks\n",
        "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/index.html)\n",
        "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/).\n",
        "\n",
        "**Module 9 Assignment: Detect Multiple Faces**\n",
        "\n",
        "**Student Name: Your Name**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "coBqKSu7lmJ4"
      },
      "source": [
        "# Assignment Instructions\n",
        "\n",
        "I provide you with five images of people in various poses, such as the following:\n",
        "\n",
        "![Multiple Faces](https://data.heatonresearch.com/images/wustl/data/AdobeStock_319245189-low.jpg)\n",
        "\n",
        "As you have seen from module 9, you can detect faces in this image, as you can see here:\n",
        "\n",
        "![Multiple Faces](https://s3.amazonaws.com/data.heatonresearch.com/images/wustl/class/bound-1.jpg)\n",
        "\n",
        "Your task for this assignment is to extract the coordinates (x,y) and dimensions (height, width). Extract the dimensions/coordinates for a rectangle around the individual face rectangles. This rectangle will overlap the edges of some of the separate face rectangles. The single rectangle would look similar, though you do not need to draw it. Rather, you will return a data frame of the coordinates and dimensions.\n",
        "\n",
        "![Multiple Faces](https://s3.amazonaws.com/data.heatonresearch.com/images/wustl/class/bound-2.jpg)\n",
        "\n",
        "Generate your dataframe from the following images.\n",
        "\n",
        "* https://data.heatonresearch.com/images/wustl/data/AdobeStock_158302589-low.jpg\n",
        "* https://data.heatonresearch.com/images/wustl/data/AdobeStock_268797955-low.jpg\n",
        "* https://data.heatonresearch.com/images/wustl/data/AdobeStock_319245189-low.jpg\n",
        "* https://data.heatonresearch.com/images/wustl/data/AdobeStock_622573012-low.jpg\n",
        "* https://data.heatonresearch.com/images/wustl/data/AdobeStock_632061559-low.jpg\n",
        "\n",
        "Your submitted dataframe should look like this. Make sure to round your numbers and convert to integer. You will need to calculate the width and height.\n",
        "\n",
        "|x|y|width|height|\n",
        "|-|-|-|-|\n",
        "|177|215|614|134|\n",
        "|316|74|472|231|\n",
        "|231|59|497|264|\n",
        "|436|160|167|245|\n",
        "|140|192|760|252|"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Z606GghplmJ5"
      },
      "source": [
        "# Google CoLab Instructions\n",
        "\n",
        "If you are using Google CoLab, it will be necessary to mount your GDrive so that you can send your notebook during the submit process. Running the following code will map your GDrive to ```/content/drive```."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "sWJdoS7ClmJ5"
      },
      "outputs": [],
      "source": [
        "try:\n",
        "    from google.colab import drive\n",
        "    drive.mount('/content/drive', force_remount=True)\n",
        "    COLAB = True\n",
        "    print(\"Note: using Google CoLab\")\n",
        "except:\n",
        "    print(\"Note: not using Google CoLab\")\n",
        "    COLAB = False\n",
        "\n",
        "# Make use of a GPU or MPS (Apple) if one is available.  (see module 3.2)\n",
        "import torch\n",
        "\n",
        "device = (\n",
        "    \"mps\"\n",
        "    if getattr(torch, \"has_mps\", False)\n",
        "    else \"cuda\"\n",
        "    if torch.cuda.is_available()\n",
        "    else \"cpu\"\n",
        ")\n",
        "print(f\"Using device: {device}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uwon6e4vlmJ6"
      },
      "source": [
        "# Assignment Submit Function\n",
        "\n",
        "You will submit the 10 programming assignments electronically.  The following submit function can be used to do this.  My server will perform a basic check of each assignment and let you know if it sees any basic problems.\n",
        "\n",
        "**It is unlikely that should need to modify this function.**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1uVC8RUTlmJ6"
      },
      "outputs": [],
      "source": [
        "import base64\n",
        "import os\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import requests\n",
        "import PIL\n",
        "import PIL.Image\n",
        "import io\n",
        "\n",
        "# This function submits an assignment.  You can submit an assignment as much as you like, only the final\n",
        "# submission counts.  The paramaters are as follows:\n",
        "# data - List of pandas dataframes or images.\n",
        "# key - Your student key that was emailed to you.\n",
        "# no - The assignment class number, should be 1 through 1.\n",
        "# source_file - The full path to your Python or IPYNB file.  This must have \"_class1\" as part of its name.\n",
        "# .             The number must match your assignment number.  For example \"_class2\" for class assignment #2.\n",
        "def submit(data,key,no,source_file=None):\n",
        "    if source_file is None and '__file__' not in globals(): raise Exception('Must specify a filename when a Jupyter notebook.')\n",
        "    if source_file is None: source_file = __file__\n",
        "    suffix = '_class{}'.format(no)\n",
        "    if suffix not in source_file: raise Exception('{} must be part of the filename.'.format(suffix))\n",
        "    with open(source_file, \"rb\") as image_file:\n",
        "        encoded_python = base64.b64encode(image_file.read()).decode('ascii')\n",
        "    ext = os.path.splitext(source_file)[-1].lower()\n",
        "    if ext not in ['.ipynb','.py']: raise Exception(\"Source file is {} must be .py or .ipynb\".format(ext))\n",
        "    payload = []\n",
        "    for item in data:\n",
        "        if type(item) is PIL.Image.Image:\n",
        "            buffered = BytesIO()\n",
        "            item.save(buffered, format=\"PNG\")\n",
        "            payload.append({'PNG':base64.b64encode(buffered.getvalue()).decode('ascii')})\n",
        "        elif type(item) is pd.core.frame.DataFrame:\n",
        "            payload.append({'CSV':base64.b64encode(item.to_csv(index=False).encode('ascii')).decode(\"ascii\")})\n",
        "    r= requests.post(\"https://api.heatonresearch.com/assignment-submit\",\n",
        "        headers={'x-api-key':key}, json={ 'payload': payload,'assignment': no, 'ext':ext, 'py':encoded_python})\n",
        "    if r.status_code==200:\n",
        "        print(\"Success: {}\".format(r.text))\n",
        "    else: print(\"Failure: {}\".format(r.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Install Facenet-Pytorch"
      ],
      "metadata": {
        "id": "ldLC0Tt2w33w"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install facenet-pytorch"
      ],
      "metadata": {
        "id": "Hn0jgYFDw6Tw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": true,
        "id": "nNXM_k8olmJ7"
      },
      "source": [
        "# Assignment #9 Sample Code\n",
        "\n",
        "The following code provides a starting point for this assignment."
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "k85pbdfSqmad"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_-HMgsaqlmJ7"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import pandas as pd\n",
        "import torch\n",
        "from facenet_pytorch import MTCNN\n",
        "from PIL import Image\n",
        "import requests\n",
        "from io import BytesIO\n",
        "import torch\n",
        "from PIL import Image, ImageDraw\n",
        "from facenet_pytorch import MTCNN\n",
        "\n",
        "# This is your student key that I emailed to you at the beginnning of the semester.\n",
        "key = \"Gx5en9cEVvaZnjut6vfLm1HG4ZO4PsI32sgldAXj\"  # This is an example key and will not work.\n",
        "\n",
        "# You must also identify your source file.  (modify for your local setup)\n",
        "file='/content/drive/My Drive/Colab Notebooks/assignment_yourname_class9.ipynb'  # Google CoLab\n",
        "# file='C:\\\\Users\\\\jeffh\\\\projects\\\\t81_558_deep_learning\\\\assignments\\\\assignment_yourname_class9.ipynb'  # Windows\n",
        "# file='/Users/jheaton/projects/t81_558_deep_learning/assignments/assignment_yourname_class9.ipynb'  # Mac/Linux\n",
        "\n",
        "# This is just a start, modify to fit the assignment\n",
        "\n",
        "mtcnn = MTCNN(keep_all=True, device=device)\n",
        "\n",
        "# Load image an image, you must process all 5\n",
        "url = \"https://data.heatonresearch.com/images/wustl/data/AdobeStock_319245189-low.jpg\"\n",
        "\n",
        "response = requests.get(url,headers={'User-Agent': 'Mozilla/5.0'})\n",
        "img = Image.open(BytesIO(response.content))\n",
        "img.load()\n",
        "\n",
        "# Detect faces\n",
        "boxes, t = mtcnn.detect(img)\n",
        "\n",
        "for box in boxes:\n",
        "  print(box)\n",
        "\n",
        "df_submit = None\n",
        "\n",
        "# Submit\n",
        "submit(source_file=file,data=[df_submit],key=key,no=9)"
      ]
    }
  ],
  "metadata": {
    "anaconda-cloud": {},
    "kernelspec": {
      "display_name": "Python 3.9 (torch)",
      "language": "python",
      "name": "pytorch"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.16"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}